package com.irrigation.controller;

import com.irrigation.service.DeviceService;
import com.irrigation.service.SensorDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
@RequiredArgsConstructor
public class WebController {

    private final DeviceService deviceService;
    private final SensorDataService sensorDataService;

    @GetMapping("/")
    public String index() {
        return "redirect:/dashboard";
    }

    @GetMapping("/dashboard")
    public String dashboard(Model model) {
        // 添加实时数据到模型
        model.addAttribute("soilMoisture", sensorDataService.getLatestSoilMoisture());
        model.addAttribute("sensorData", sensorDataService.getSensorDataHistory());
        // 假设主水泵的ID为1L
        model.addAttribute("mainPumpStatus", deviceService.getDeviceStatus(1L));
        model.addAttribute("mainPumpRunning", deviceService.isDeviceRunning(1L));
        
        return "dashboard";
    }

    @GetMapping("/strategy")
    public String strategy(Model model) {
        // 添加灌溉策略相关数据
        return "strategy";
    }

    @GetMapping("/devices")
    public String devices(Model model) {
        model.addAttribute("devices", deviceService.getAllDevices());
        return "devices";
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }
} 